package com.yanqu.road.server.pb;
import com.yanqu.road.entity.activity.ruishou.RuiShouAreaData;
import com.yanqu.road.entity.activity.ruishou.RuiShouFightMonsterData;
import com.yanqu.road.entity.activity.ruishou.RuiShouFightMonsterDeadData;
import com.yanqu.road.entity.activity.ruishou.RuiShouMonsterData;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouActionConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouAreaConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouMonsterConfig;
import com.yanqu.road.pb.activity.RuiShouProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.Map;

public class RuiShouPb {

    public static RuiShouProto.RuiShouAreaConfigMsg.Builder parseRuiShouAreaConfigMsg(RuiShouAreaConfig config, String language) {
        if (config == null) {
            return null;
        }
        RuiShouProto.RuiShouAreaConfigMsg.Builder builder = RuiShouProto.RuiShouAreaConfigMsg.newBuilder();
        builder.setArea(config.getArea());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setUnlock(config.getUnlock());
        builder.setNum(config.getNum());
        builder.setScore(config.getScore());
        return builder;
    }

    public static RuiShouProto.RuiShouMonsterConfigMsg.Builder parseRuiShouMonsterConfigMsg(RuiShouMonsterConfig config, String language) {
        if (config == null) {
            return null;
        }
        RuiShouProto.RuiShouMonsterConfigMsg.Builder builder = RuiShouProto.RuiShouMonsterConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setArea(config.getArea());
        builder.setType(config.getType());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setLevel(config.getLevel());
        builder.setAward(config.getAward());
        builder.setRandomAward(config.getRandomAward());
        builder.setNum(config.getNum());
        builder.setWeight(config.getWeight());
        return builder;
    }

    public static RuiShouProto.RuiShouAreaDataMsg.Builder parseRuiShouAreaDataMsg(RuiShouAreaData areaData) {
        if (areaData == null) {
            return null;
        }
        RuiShouProto.RuiShouAreaDataMsg.Builder builder = RuiShouProto.RuiShouAreaDataMsg.newBuilder();
        builder.setId(areaData.getAreaId());
        // 怪物出战次数
        for (RuiShouMonsterData monsterData : areaData.getMonsterDataMap().values()) {
            RuiShouProto.RuiShouChooseMonsterDataMsg.Builder chooseMonsterTemp = RuiShouProto.RuiShouChooseMonsterDataMsg.newBuilder();
            chooseMonsterTemp.setId(monsterData.getMonsterId());
            chooseMonsterTemp.setFightCnt(monsterData.getFightCnt());
            builder.addChooseMonster(chooseMonsterTemp);
        }
        for (RuiShouFightMonsterData fightMonsterData : areaData.getFightMonsterDataMap().values()) {
            if (fightMonsterData.getId() == 0 || fightMonsterData.getHp() == 0) {
                continue;
            }
            builder.addFightMonster(parseRuiShouFightMonsterDataMsg(fightMonsterData));
        }
        builder.setCanReset(areaData.isReset());
        builder.setCanChoose(!areaData.isLockReward());
        // 自选奖励情况
        for (RuiShouMonsterData monsterData : areaData.getMonsterDataMap().values()) {
            for (Map.Entry<Integer, Integer> entry : monsterData.getChooseAwardCntMap().entrySet()) {
                RuiShouProto.RuiShouMonsterAwardDataMsg.Builder awardTemp = RuiShouProto.RuiShouMonsterAwardDataMsg.newBuilder();
                awardTemp.setId(monsterData.getMonsterId());
                awardTemp.setRewardId(entry.getKey());
                awardTemp.setChooseCnt(entry.getValue());
                builder.addAward(awardTemp);
            }
        }
        // 已选奖励
        for (RuiShouMonsterData monsterData : areaData.getMonsterDataMap().values()) {
            RuiShouProto.RuiShouMonsterRewardDataMsg.Builder rewardTemp = RuiShouProto.RuiShouMonsterRewardDataMsg.newBuilder();
            rewardTemp.setId(monsterData.getMonsterId());
            rewardTemp.setRewardId(monsterData.getChooseRewardId());
            builder.addChooseReward(rewardTemp);
        }
        return builder;
    }

    private static RuiShouProto.RuiShouFightMonsterDataMsg.Builder parseRuiShouFightMonsterDataMsg(RuiShouFightMonsterData fightMonsterData) {
        if (fightMonsterData == null) {
            return null;
        }
        RuiShouProto.RuiShouFightMonsterDataMsg.Builder builder = RuiShouProto.RuiShouFightMonsterDataMsg.newBuilder();
        builder.setId(fightMonsterData.getId());
        builder.setPos(fightMonsterData.getPos());
        builder.setHp(fightMonsterData.getHp());
        builder.setReward(fightMonsterData.getReward());
        return builder;
    }

    public static RuiShouProto.RuiShouShootMonsterDeadDataMsg.Builder parseRuiShouShootMonsterDeadDataMsg(RuiShouFightMonsterDeadData deadData) {
        if (deadData == null) {
            return null;
        }
        RuiShouProto.RuiShouShootMonsterDeadDataMsg.Builder builder = RuiShouProto.RuiShouShootMonsterDeadDataMsg.newBuilder();
        builder.setId(deadData.getId());
        builder.setPos(deadData.getPos());
        builder.setReward(deadData.getReward());
        return builder;
    }


    public static RuiShouProto.RuiShouActionConfigMsg.Builder parseRuiShouActionConfigMsg(RuiShouActionConfig actionConfig) {
        if (actionConfig == null) {
            return null;
        }
        RuiShouProto.RuiShouActionConfigMsg.Builder builder = RuiShouProto.RuiShouActionConfigMsg.newBuilder();
        builder.setId(actionConfig.getId());
        builder.addAllMovePath(actionConfig.getMovePath());
        builder.setReturnPos(actionConfig.getReturnPos());
        builder.setActionData(actionConfig.getActionData());
        return builder;
    }
}
